#!/bin/bash

#游戏服务器部署脚本
#游戏服需运行在centOS上

###########################
#一些需要设置的参数

REDIS_BINDIP="127.0.0.1 192.168.1.72"	#redis绑定的主机ip
SVRIP="g.nen6123456.cn"				#游戏服ip地址

###########################

ROOTPATH=`pwd`

echo "-----------------start deploy gameserver -------------------------"

#1、安装mysql
mkdir -p /data/mysqldb
cd /data/mysqldb

echo "----------------start install mysql---------------"
#yum install mariadb-server mariadb
#systemctl enable mariadb  #设置开机启动
#mysql_install_db --user=mysql --basedir=/usr --datadir=/data/mysqldb #安装mysql实例,将安装一个mysql系统库
#mysqld_safe --user=mysql --datadir=/data/mysqldb & #安全模式启动MariaDB
#source $ROOTPATH/app/dbtool/allgamedb.env
#mysqladmin -u root password $DBPASS	#设置root用户密码
echo "----------------end install mysql---------------"

#2、安装redis
mkdir -p /data/redisdb
cd /data/redisdb

echo "----------------start install redis---------------"
yum install redis-server
CONFIGFILE=`find / -name 'redis.conf' | head -n 1`
perl -pi -e "s|^daemonize no|daemonize yes|" $CONFIGFILE
#perl -pi -e "s|^bind.*|bind $REDIS_BINDIP|" $CONFIGFILE	#绑定主机ip，可绑定多个
perl -pi -e "s|^dir.*|dir /data/redisdb/|" $CONFIGFILE
perl -pi -e "s|^logfile.*|logfile \"/data/redisdb/redis.log\"|" $CONFIGFILE
#sed -i "s/^daemonize no/daemonize yes/" $CONFIGFILE
#sed -i "s/^bind.*/bind $REDIS_BINDIP/" $CONFIGFILE
redis-server $CONFIGFILE
echo "----------------end install redis---------------"

#3、skynet环境搭建
cd $ROOTPATH
. ./build.sh

#4、游戏服环境配置
echo "----------------start init game environment---------------"
sed -i "s/link_ip.*/link_ip = \"$SVRIP\",/" $ROOTPATH/app/config/config_svrs
sed -i "s/relpay_logpath.*/relpay_logpath = \"http:\/\/$SVRIP\/\",/" $ROOTPATH/app/config/config_svrs
sed -i "s/local svr_ip =.*/local svr_ip = \"$SVRIP\"/" $ROOTPATH/app/common/lib/run_check.lua
sed -i "s/local relpay_logpath =.*/local relpay_logpath = \"http:\/\/$SVRIP\/\"/" $ROOTPATH/app/common/lib/run_check.lua

cd $ROOTPATH/app/dbtool
. ./create_allgame_table.sh

cd ../gmnode
. ./cmd.sh

#5、创建机器人
robotnum=`mysql -uroot -p $DBPASS -D $DBNAMES -se "select count(*) as num from rs_robot" | head`
if [[ -z "$robotnum" ]]; then
	echo "access database failed!"
elif [[ $robotnum -gt 2 ]]; then
	echo "robot is already build!"
else
	cd $ROOTPATH
	. ./run.sh
	sleep 5
	redis-cli -n 15 rpush requestmq batch_build_robots

	echo "----------------start build robot---------------"
	DATEDIR=$(date "+%Y_%m_%d")
	cd $ROOTPATH/applog/$DATEDIR

	isok=""
	while [[ -z "$isok" ]]
	do
		echo "please waiting, robot is building..."
		sleep 5
		isok=$(cat robotnode_info.log | grep "build robots ok")
	done
	echo $isok
	echo "----------------end build robot---------------"

	cd $ROOTPATH
	yum install psmisc
	killall skynet
fi

#6、启动游戏
. ./run.sh

echo "-----------------end deploy gameserver -------------------------"








